package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.DeliveryType;
import domain.Mission;
import domain.POS;
import domain.Route;
import domain.Schedule;
import util.DateUtil;;

/**
 * Servlet implementation class GetScheduleController
 */
public class GetScheduleController extends HttpServlet {
	private EntityManager entityManager;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetScheduleController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Schedule schedule = new Schedule();
			schedule.setRoute(Route.R12);
			schedule.setStartTime(DateUtil.getCurrentTimestamp());
			schedule.setEndTime(DateUtil.getCurrentTimestamp());
			
		POS pos = new POS();
			pos.setName("BTNC heuartead qr");
			pos.setLatitute(13.711428);
			pos.setLongtitute(100.594103);
			
		POS pos2 = new POS();
			pos2.setName("The Mall Ramkhamhaeng 3");
			pos2.setLatitute(13.752729);
			pos2.setLongtitute(100.609644);
			
		POS pos3 = new POS();
			pos3.setName("Seconsquare");
			pos3.setLatitute(13.695265);
			pos3.setLongtitute(100.648187);
			
		POS pos4 = new POS();
			pos4.setName("Central Department Store");
			pos4.setLatitute(13.669594);
			pos4.setLongtitute(100.634778);
			
		Mission mission = new Mission();
			mission.setFromPOS(pos);
			mission.setBrandName("Guy Laroche");
			mission.setToPOS(pos2);
			mission.setPriority(1);
			mission.setNoOfProduct(200);
			mission.setDevType(DeliveryType.refill);
		
		Mission mission2 = new Mission();
			mission2.setFromPOS(pos2);
			mission2.setBrandName("Jousse");
			mission2.setToPOS(pos3);
			mission2.setPriority(2);
			mission2.setNoOfProduct(150);
			mission2.setDevType(DeliveryType.refill);
		
		Mission mission3 = new Mission();
			mission3.setFromPOS(pos3);
			mission3.setToPOS(pos4);
			mission3.setPriority(3);
			mission3.setDevType(DeliveryType.getBill);
		
		
			
		schedule.addMission(mission);
		schedule.addMission(mission2);
		schedule.addMission(mission3);
		
		request.setAttribute("schedule", schedule);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/getSchedule.jsp");
		dispatcher.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	public Collection<POS> getPOS(){
		Query query = entityManager.createQuery("SELECT c FROM Customer AS c");
    	@SuppressWarnings("unchecked")
        List<POS> customers = query.getResultList();
        return customers;
	}

}
